package ru.spbu.math.android.cbrf.view;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import ru.spbu.math.android.cbrf.R;
import ru.spbu.math.android.cbrf.dataprovider.ProviderManager;
import ru.spbu.math.android.cbrf.model.RateType;

import android.content.Context;
import android.database.DataSetObserver;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.TextView;

/**
 * This Class is ListAdapter for RateTypes list dialog. Also it watches over
 * selection of RateTypes in list.
 * 
 * @author Olga Nikonova
 */
public class RateTypeListAdapter implements ListAdapter {

	private List<RateType> rateTypes;
	private Context context;
	private CategoryListAdapter categoryListAdapter;
	private Map<RateType, View> views;

	/**
	 * set this RateType as selected in categoryListAdapter and set view,
	 * related to it, as active.
	 * 
	 * @param selectedRateType
	 */
	public void setSelectedRateType(RateType selectedRateType) {
		categoryListAdapter.setSelectedRateType(selectedRateType);

		View view = views.get(selectedRateType);
		ImageView imView = ((ImageView) view.findViewById(R.id.item_icon));
		imView.setImageResource(R.drawable.list_item_icon_active);
	}

	public RateTypeListAdapter(Context context, List<RateType> rateTypes,
			CategoryListAdapter categoryListAdapter) {
		this.rateTypes = rateTypes;
		this.context = context;
		this.categoryListAdapter = categoryListAdapter;

		views = new HashMap<RateType, View>();
		for (RateType rateType : rateTypes) {
			views.put(rateType, makeViewFromRateType(rateType));
		}
	}

	@Override
	public int getCount() {
		return rateTypes.size();
	}

	@Override
	public Object getItem(int position) {
		return rateTypes.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public int getItemViewType(int position) {
		return ListAdapter.IGNORE_ITEM_VIEW_TYPE;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		return views.get(rateTypes.get(position));
	}

	@Override
	public int getViewTypeCount() {
		return 1;
	}

	@Override
	public boolean hasStableIds() {
		return false;
	}

	@Override
	public boolean isEmpty() {
		return rateTypes.isEmpty();
	}

	@Override
	public void registerDataSetObserver(DataSetObserver observer) {

	}

	@Override
	public void unregisterDataSetObserver(DataSetObserver observer) {

	}

	@Override
	public boolean areAllItemsEnabled() {
		return true;
	}

	@Override
	public boolean isEnabled(int position) {
		return true;
	}

	/**
	 * @param rateType
	 * @return view represent this rateType
	 */
	private View makeViewFromRateType(RateType rateType) {
		View res = LayoutInflater.from(context).inflate(
				R.layout.ratetype_list_item, null);

		ImageView imView = ((ImageView) res.findViewById(R.id.item_icon));
		if (categoryListAdapter.getSelectedRateType() != null
				&& categoryListAdapter.getSelectedRateType().equals(rateType)) {
			imView.setImageResource(R.drawable.list_item_icon_active);
		} else {
			imView.setImageResource(R.drawable.list_item_icon_inactive);
		}

		((TextView) res.findViewById(R.id.toptext)).setText(rateType.getName());
		((TextView) res.findViewById(R.id.bottomtext)).setText(ProviderManager
				.getInstance(context).getProviderName(rateType));

		return res;
	}
}
